#include "clientmanagement.h"
#include "app_mysql.h"
#include "DataStruct.h"
#include <iostream>
#include <list>

using namespace std;

ClientManagement::ClientManagement()
{
}

Text ClientManagement::login(QString userid, QString psw)
{
    cout << "login" << endl;
    Text t;
    User user;

    char id[userid.length()+1];
    memcpy(id,userid.toLocal8Bit().data(),userid.length());
    id[userid.length()] = '\0';

    cout << "id:" <<id << endl;
    cout << "psw:" <<psw.toStdString() << endl;

    database_start("root","");
    user = queryUser_mysql(id);
    database_end();

    cout << "id:" << user.ID.toLocal8Bit().data() << " name:" << user.name.toLocal8Bit().data() << " psw:" << user.password.toLocal8Bit().data() << endl;

    if(user.password == psw.trimmed()) {
        t.add(user.ID);
        t.add(user.name);
        return t;
    } else
        return t;
}

void ClientManagement::modifyUser(QString userid, QString username, QString psw) {
    char idBuff[userid.length()+1];
    memcpy(idBuff,userid.toLocal8Bit().data(),userid.length());
    idBuff[userid.length()] = '\0';

    char nameBuff[username.length()+1];
    memcpy(nameBuff,username.toLocal8Bit().data(),username.length());
    nameBuff[username.length()] = '\0';

    char pswBuff[psw.length()+1];
    memcpy(pswBuff,psw.toLocal8Bit().data(),psw.length());
    pswBuff[psw.length()] = '\0';

    modifyUser_mysql(idBuff,nameBuff,pswBuff);

}

Text ClientManagement::modify(QString userid, QString username, QString psw)
{
    Text t;
    User user;

    char idBuff[userid.length()+1];
    memcpy(idBuff,userid.toLocal8Bit().data(),userid.length());
    idBuff[userid.length()] = '\0';

    database_start("root","");
    user = queryUser_mysql(idBuff);

    if (user.ID.isEmpty()) {
        t.setType(ERRO_CODE);
        return t;
    }

    if (psw.isEmpty()) {
        modifyUser(userid,username,user.password);
    } else if (username.isEmpty()) {
        modifyUser(userid,user.name,psw);
    } else {
        modifyUser(userid,username,psw);
    }
    database_end();
    return t;

}

Text ClientManagement::query(QString start, QString arrival, QString date)
{
    Station startStation;
    Station arrivalStation;
    startStation.name = start;
    arrivalStation.name = arrival;

    char buff[date.length()+1];
    memcpy(buff,date.toLocal8Bit().data(),date.length());
    buff[date.length()] = '\0';

    list<TicketRecord> ticketRecord;
    list<TicketRecord>::iterator i;
    Text t;

    cout << buff << endl;

    database_start("root","");

    startStation.ID = queryStation_mysql(start.toLocal8Bit().data());
    arrivalStation.ID = queryStation_mysql(arrival.toLocal8Bit().data());
    ticketRecord = queryTicketRecord_mysql(startStation.ID,arrivalStation.ID,buff);

    cout << startStation.ID << arrivalStation.ID << buff << endl;

    for (i = ticketRecord.begin(); i != ticketRecord.end(); ++i) {
        t.add(QString::number((*i).ID));
        t.add(QString((*i).trainNum));
        t.add(start);
        t.add("12:23:34");
        t.add(arrival);
        t.add("23:23:23");
        t.add(QString::number((*i).price));
        t.add(QString::number((*i).restNum));
    }

    database_end();

    return t;

}

Text ClientManagement::purchase(QString userid, QString ticketID, QString date)
{
    Text text;
    TicketRecord ticketRecord;

    cout << ticketRecord.restNum << endl;

    char dateBuff[date.length()+1];
    memcpy(dateBuff,date.toLocal8Bit().data(),date.length());
    dateBuff[date.length()] = '\0';

    char userBuff[userid.length()+1];
    memcpy(userBuff,userid.toLocal8Bit().data(),userid.length());
    userBuff[userid.length()] = '\0';

    int ticketType = ticketID.toInt();

    database_start("root","");
    ticketRecord = queryRestNum_mysql(ticketType,dateBuff);

    char trainNumBuff[ticketRecord.trainNum.length()+1];
    memcpy(trainNumBuff,ticketRecord.trainNum.toLocal8Bit().data(),ticketRecord.trainNum.length());
    trainNumBuff[ticketRecord.trainNum.length()] = '\0';

    cout << ticketRecord.restNum << endl;

    if (ticketRecord.restNum > 0) {
        addTicket_mysql(ticketType,dateBuff,trainNumBuff,ticketRecord.startStation,ticketRecord.arrivalStation,ticketRecord.price,"stand",userBuff);
        modifyTicketRecord_mysql(ticketRecord.ID,ticketRecord.restNum-1);
        text.setType(LOGIN_CODE);
    } else {
        text.setType(ERRO_CODE);
    }

    database_end();

    return text;
}

Text ClientManagement::queryHistory(QString userid)
{
    Station startStation;
    Station arrivalStation;

    char userBuff[userid.length()+1];
    memcpy(userBuff,userid.toLocal8Bit().data(),userid.length());
    userBuff[userid.length()] = '\0';

    list<TrainTicket> tickets;
    list<TrainTicket>::iterator i;
    Text t;

    database_start("root","");

    tickets = queryTicket_mysql(userBuff);

    for (i = tickets.begin(); i != tickets.end(); ++i) {
        startStation.ID = (*i).startStation;
        arrivalStation.ID = (*i).arrivalStation;
        startStation.name = QString(queryStation_mysql(startStation.ID));
        arrivalStation.name = QString(queryStation_mysql(arrivalStation.ID));

        t.add(QString::number((*i).ID));
        t.add((*i).trainNum);
        t.add((*i).date);
        t.add(startStation.name);
        t.add((*i).startTime);
        t.add(arrivalStation.name);
        t.add((*i).arrivalTime);
        t.add(QString::number((*i).price));
    }

    database_end();

    return t;

}

Text ClientManagement::cancell(QString ticketID)
{
    Text text;
    int id = ticketID.toInt();
    TrainTicket ticket;
    TicketRecord ticketRecord;
    int ticketType;

    database_start("root","");
    ticket = queryTicketTypeDate_mysql(id);
    ticketType = ticket.ticketType;

    char dateBuff[ticket.date.length()+1];
    memcpy(dateBuff,ticket.date.toLocal8Bit().data(),ticket.date.length());
    dateBuff[ticket.date.length()] = '\0';

    ticketRecord = queryRestNum_mysql(ticketType,dateBuff);

    if (deleteTicket_mysql(id)) {
        modifyTicketRecord_mysql(ticketRecord.ID,ticketRecord.restNum+1);
        text.setType(LOGIN_CODE);
    } else {
        text.setType(ERRO_CODE);
    }

    return text;

}
